<!DOCTYPE html>
<title>[LegacyLenientThis] test</title>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script>
test(function() {
  // GlobalEventHandlers.onmouseenter is [LegacyLenientThis].
  assert_equals(document.onmouseenter, null, "|this| is an appropriate object.");
  assert_equals(Document.prototype.onmouseenter, undefined, "|this| is not an appropriate object.");
  Document.prototype.onmouseenter = 'foo'; // must not throw.
  assert_equals(Document.prototype.onmouseenter, undefined, "the value must not change.");

  // GlobalEventHandlers.onmousedown is NOT [LegacyLenientThis].
  assert_equals(document.onmousedown, null, "|this| is an appropriate object.");
  assert_throws_js(TypeError, function() {
    Document.prototype.onmousedown;
  }, "Document.prototype is invalid as |this|.");
  assert_throws_js(TypeError, function() {
    Document.prototype.onmousedown = 'foo';
  }, "Document.prototype is invalid as |this|.");
}, "Test [LegacyLenientThis] and non-[LegacyLenientThis].");

test(function() {
  var noop = function() {};
  var testPatterns = [
    {name: 'document', instance: document, holder: Document.prototype, noninstance: window},
    {name: 'window',  instance: window, holder: window, noninstance: document}
  ];
  function msg(pattern, text) {
    return '[' + pattern.name + '] ' + text;
  }
  for (var p, i = 0; p = testPatterns[i]; ++i) {
    var pd = Object.getOwnPropertyDescriptor(p.holder, 'onmouseleave');
    // get
    assert_equals(pd.get.call(p.noninstance), undefined, msg(p, 'get: non-instance should return undefined.'));
    assert_equals(pd.get.call(p.instance), null, msg(p, 'get: instance should return null by default.'));

    // set
    assert_equals(pd.set.call(p.noninstance, noop), undefined, msg(p, 'set: setter always returns undefined.'));
    assert_equals(pd.get.call(p.noninstance), undefined, msg(p, 'set: non-instance should returns undefined.'));
    assert_equals(pd.set.call(p.instance, noop), undefined, msg(p, 'set: even instance should return undefined.'));
    assert_equals(pd.get.call(p.instance), noop, msg(p, 'set: instance should return the assigned function.'));
  }

  // document specific tests.
  pd = Object.getOwnPropertyDescriptor(Document.prototype, 'onmouseleave');
  assert_equals(pd.get.call(null), undefined, '[document] get: null is not a valid instance.');
  assert_equals(pd.get.call(undefined), undefined, '[document] get: undefined is not a valid instance.');

  // window specific tests.
  pd = Object.getOwnPropertyDescriptor(window, 'onmouseleave');
  assert_equals(pd.get.call(null), noop, '[window] get: null should be interpreted as the global object.');
  assert_equals(pd.get.call(undefined), noop, '[window] get: undefined should be interpreted as the global object.');
  assert_equals(pd.set.call(undefined, null), undefined, '[window] set: returns undefined.');
  assert_equals(pd.get.call(undefined), null, '[window] set: should be updated.');
}, "Test with extracted accessors.");
</script>
